package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ViolationProjectDTO;
import com.eastfair.venueservice.dto.ViolationProjectPageQuery;
import com.eastfair.venueservice.dto.ViolationProjectSaveDTO;
import com.eastfair.venueservice.dto.ViolationProjectUpdateDTO;
import com.eastfair.venueservice.entity.ViolationProject;
import com.eastfair.venueservice.service.ViolationProjectService;
import com.eastfair.venueservice.vo.ViolationProjectVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 违规项目
 * </p>
 *
 * @author dqq
 * @date 2023-03-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/violationProject")
@Api(value = "ViolationProject", tags = "违规项目")
@PreAuth(replace = "venueservice:violationProject:")
public class ViolationProjectController extends SuperController<ViolationProjectService, Long, ViolationProject, ViolationProjectPageQuery, ViolationProjectSaveDTO, ViolationProjectUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ViolationProjectService violationProjectService;

    @Override
    public void handlerResult(IPage<ViolationProject> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ViolationProject> violationProjectList = list
                .stream()
                .map((map) -> ViolationProject.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(violationProjectList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ViolationProjectPageQuery> params) {
        ViolationProjectPageQuery pageQuery = params.getModel();
    }

    /**
     * 场馆查询违规项目.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/violationProject")
    public R<Page<ViolationProjectVO>> queryForVenue(@RequestBody PageParams<ViolationProjectDTO> pageParams) {
        log.info("queryViolationProjectVO - 场馆查询违规项目, pageParams={}", pageParams);
        return R.successDef(violationProjectService.listForVenue(pageParams));
    }

    /**
     * 主场查询违规项目.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/for/home")
    public R<Page<ViolationProjectVO>> queryForHome(@RequestBody PageParams<ViolationProjectDTO> pageParams) {
        log.info("queryViolationProjectVO - 主场查询违规项目, pageParams={}", pageParams);
        return R.successDef(violationProjectService.listForHome(pageParams));
    }

    /**
     * id获取违规项目.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<ViolationProjectVO> getById(@PathVariable("id") Long id) {
        return R.successDef(violationProjectService.getVOById(id));
    }
}
